package org.hoyi.util;

public class EncodeUtil
{
	public static String tranEncodeToUTF8(String str)
	{
		try
		{
			String strEncode = getEncoding(str);
			String temp = new String(str.getBytes(strEncode), "UTF-8");
			return temp;
		}
		catch (java.io.IOException ex)
		{
			return null;
		}
	}

	/**
	 * 判断字符串的编码
	 *
	 * @param value
	 * @return value
	 */
	public static String getEncoding(String value)
	{
		String encode = "ISO-8859-1";
		try
		{
			if (value.equals(new String(value.getBytes(encode), encode)))
			{
				return "ISO-8859-1";
			}
		}
		catch (Exception exception)
		{
		}
		encode = "UTF-8";
		try
		{
			if (value.equals(new String(value.getBytes(encode), encode)))
			{
				return "UTF-8";
			}
		}
		catch (Exception exception1)
		{
		}
		encode = "GB2312";
		try
		{
			if (value.equals(new String(value.getBytes(encode), encode)))
			{
				return "GB2312";
			}
		}
		catch (Exception exception2)
		{
		}
		encode = "GBK";
		try
		{
			if (value.equals(new String(value.getBytes(encode), encode)))
			{
				return "GBK";
			}
		}
		catch (Exception exception3)
		{
		}
		return "";
	}
}
